type AnyObject<T = unknown> = { [key: string]: T };

export const cloneDeep = <T>(value: T): T => {
  // 处理基本数据类型
  if (typeof value !== 'object' || value === null) {
    return value;
  }

  // 处理日期对象
  if (value instanceof Date) {
    return new Date(value.getTime()) as T;
  }

  // 处理正则表达式对象
  if (value instanceof RegExp) {
    return new RegExp(value) as T;
  }

  // 处理数组
  if (Array.isArray(value)) {
    return value.map((item) => cloneDeep(item)) as T;
  }

  // 处理普通对象
  const obj: AnyObject = {};
  for (const key in value) {
    if (Object.prototype.hasOwnProperty.call(value, key)) {
      obj[key] = cloneDeep(value[key]);
    }
  }

  return obj as T;
};
